<template>
  <div class="main-app">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="form-inline">
        <!-- <el-form-item label="车辆名称:">
          <el-input v-model="formInline.name" clearable />
        </el-form-item> -->
        <el-form-item label="车牌号:">
          <el-input v-model="formInline.number" clearable />
        </el-form-item>
        <el-form-item label="状态:">
          <el-select
            v-model="formInline.status"
            placeholder="请选择状态"
            clearable
          >
            <el-option label="空闲" value="0" />
            <el-option label="忙碌" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="show-result">
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button icon="el-icon-arrow-left" @click="cancel">
            返 回
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" @click="add">
            新增
          </el-button>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-edit"
            :disabled="disabled"
            @click="edit"
          >
            编辑
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            :disabled="deleteDisabled"
            @click="deleteCar"
          >
            删除
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-view"
            :disabled="disabled"
            @click="view"
          >
            查看详情
          </el-button>
        </el-form-item>
      </el-form>

      <div class="result" style="margin-top: -10px">
        <el-table
          ref="multipleTable"
          v-loading="loading"
          border
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          align="center"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <!-- <el-table-column prop="name" label="车辆名称" /> -->
          <el-table-column prop="plateNumber" label="车牌号"  />
          <!-- <el-table-column label="车牌颜色">
            <template slot-scope="scope">
              <el-tag size="medium">{{
                scope.row.plateColor | parsePlateColor
              }}</el-tag>
            </template>
          </el-table-column> -->

          <el-table-column label="车牌颜色" prop="plateColor" width="120">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" effect="dark" :class="'bg-'+scope.row.plateColorColor">{{
            scope.row.plateColorName
          }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="车辆类型" prop="vehicleModel" width="120">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" effect="dark" :class="'bg-'+scope.row.vehicleModelColor">{{
            scope.row.vehicleModelName
          }}</el-tag>
            </template>
          </el-table-column>

          <!-- <el-table-column label="车辆类型">
            <template slot-scope="scope">
              <el-tag size="medium">{{
                scope.row.vehicleModel | parseVehicleModel
              }}</el-tag>
            </template>
          </el-table-column> -->


          <el-table-column prop="authorizedCapacity" label="核定载人数" />
          <el-table-column prop="approvedLoadMass" label="核定载质量" />
          <!-- <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <el-tag size="medium">{{
                scope.row.status | parseStatus
              }}</el-tag>
            </template>
          </el-table-column> -->

          <el-table-column label="状态" prop="status" width="120">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" effect="dark" :class="'bg-'+scope.row.statusColor">{{
            scope.row.statusName
          }}</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="使用状态" prop="useStatus" width="120">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" effect="dark" :class="'bg-'+scope.row.useStatusColor">{{
            scope.row.useStatusName
          }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="强制报废期">
            <template slot-scope="scope">
              {{ scope.row.eomrpTime | parseDate }}
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination
            background
            :current-page.sync="currentPage"
            layout="total,prev, pager, next, sizes"
            :total="total"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { CarType, LicensePlateColor,EquipmentStatus,
CarState} from "../../../../utils/resource";
import { pageMixin } from "../../../../mixins/index";
import { getList, remove } from "../../../../api/vehicle";
export default {
  filters: {
    parseVehicleModel(value) {
      return CarType.find((p) => p.value === value)?.label;
    },
    parsePlateColor(value) {
      return LicensePlateColor.find((p) => p.value === value)?.label;
    },
    parseUseStatus(value) {
      return value === "0" ? "启用" : "未启用";
    },
    parseStatus(value) {
      return value === "0" ? "空闲" : "忙碌";
    },
    parseDate(value) {
      if (value) {

        return value.substring(0, value.indexOf("T"));
      }
      return null;
    },
  },
  mixins: [pageMixin],
  data() {
    return {
      formInline: {
        name: "",
        number: "",
        status: "",
      },
      carId: "",
      orgId:""
    };
  },
  async created() {
    const { id } = this.$route.params;
    this.orgId = id;
    await this.init();

  },
  methods: {
    deleteCar() {
      this.$confirm("此操作将永久删除这些车辆, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async() => {
        const ids = this.ids.map((p) => p.id);
        await remove(ids);
        await this.init();
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
    },
    view() {
      this.$router.push(
        `/organization/transport/car/${this.orgId}/view/${this.id}`
      );
    },
    edit() {
      this.$router.push(
        `/organization/transport/car/${this.orgId}/edit/${this.id}`
      );
    },
    add() {
      this.$router.push(`/organization/transport/car/${this.orgId}/create`);
    },
    async reset() {
      this.formInline = {
        name: "",
        number: "",
        status: "",
      };
      await this.init();
    },
    cancel() {
      this.$router.push("/organization/transport");
    },
    async init() {
      this.tableData = [];
      this.loading = true;
      const { data, count } = await getList(this.orgId, {
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        name: this.formInline.name,
        plateNumber: this.formInline.number,
        status: this.formInline.status,
      });
      this.tableData = data;
      this.tableData.forEach(p=>{


        var obj = LicensePlateColor.find((MM) => MM.value === p.plateColor.toString());
        if (obj) {
          p.plateColorName = obj.label;
          p.plateColorColor = obj.color;
        } else {
          p.plateColorName = "无数据";
          p.plateColorColor = "red";
        }

        var obj = CarType.find((MM) => MM.value === p.vehicleModel.toString());
        if (obj) {
          p.vehicleModelName = obj.label;
          p.vehicleModelColor = obj.color;
        } else {
          p.vehicleModelName = "无数据";
          p.vehicleModelColor = "red";
        }

        var obj = EquipmentStatus.find((MM) => MM.value === p.useStatus.toString());
        if (obj) {
          p.useStatusName = obj.label;
          p.useStatusColor = obj.color;
        } else {
          p.useStatusName = "无数据";
          p.useStatusColor = "red";
        }

        var obj = CarState.find((MM) => MM.value === p.status.toString());
        if (obj) {
          p.statusName = obj.label;
          p.statusColor = obj.color;
        } else {
          p.statusName = "无数据";
          p.statusColor = "red";
        }
      })
      this.total = count;
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.result {
  margin-top: 10px;
}

.show-result {
  margin-top: 10px;
}

.abow_dialog {
  display: flex;
  justify-content: center;
  align-items: Center;
  overflow: hidden;
  .el-dialog {
    margin: 0 auto !important;
    height: 90%;
    overflow: hidden;
    .el-dialog__body {
      position: absolute;
      left: 0;
      top: 54px;
      bottom: 0;
      right: 0;
      padding: 0;
      z-index: 1;
      overflow: hidden;
      overflow-y: auto;
    }
  }
}
</style>
